package com.ingesup.controllers
{
	import com.ingesup.entities.ListeDeDiffusion;
	import com.ingesup.models.AppModel;
	
	import mx.collections.ArrayCollection;
	import mx.controls.Alert;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.remoting.RemoteObject;
	
	import org.swizframework.utils.services.ServiceHelper;
	
	
	public class ListeDeDiffusionController
	{		
		[Inject]
		public var serviceHelper:ServiceHelper;
		
		public var listeSrv:RemoteObject;
		
		public function ListeDeDiffusionController()
		{
		}
		
		[EventHandler (event="ListeDeDiffusionEvent.GET_ALL")]
		public function getAll ():void 
		{
			serviceHelper.executeServiceCall(listeSrv.getListListeDeDiffusion(),
				
				function handleGetAllResult(event:ResultEvent):void
				{
					AppModel.listeListesDeDiffusion = event.result as ArrayCollection;
				},
				
				function handleGetAllFault(event:FaultEvent):void 
				{
					Alert.show(event.fault.message + " " + event.fault.faultDetail);
				}
			);
		}
		
		
		[EventHandler (event="ListeDeDiffusionEvent.GET", properties="id")]
		public function get (id:Number):void 
		{
			serviceHelper.executeServiceCall(listeSrv.getListeDeDiffusion(id),
				
				function handleGetAllResult(event:ResultEvent):void
				{
					AppModel.contactCourant.listeDeDiffusion = event.result as ArrayCollection;
				},
				
				function handleGetAllFault(event:FaultEvent):void 
				{
					Alert.show(event.fault.message + " " + event.fault.faultDetail);
				}
			);
		}
		
		
		[EventHandler (event="ListeDeDiffusionEvent.MODIFIER", properties="listeDeDiffusion")]
		public function update (dataToUpdate:ListeDeDiffusion):void 
		{
			serviceHelper.executeServiceCall(listeSrv.updateListeDeDiffusion(dataToUpdate),
				
				function handleGetAllResult(event:ResultEvent):void
				{
					//AppModel.listeDeDiffusionCourante = null;
				},
				
				function handleGetAllFault(event:FaultEvent):void 
				{
					Alert.show(event.fault.message + " " + event.fault.faultDetail);
				}
			);
		}
		
		[EventHandler (event="ListeDeDiffusionEvent.ENREGISTRER", properties="listeDeDiffusion")]
		public function save (dataToSave:ListeDeDiffusion):void 
		{	
			serviceHelper.executeServiceCall(listeSrv.createListeDeDiffusion(dataToSave),
				
				function handleSaveResult(event:ResultEvent):void 
				{
					AppModel.listeListesDeDiffusion.addItem(event.result as ListeDeDiffusion);
					//AppModel.listeDeDiffusionCourante = null;
				},
				
				function handleSaveFault(event:FaultEvent):void 
				{
					trace("ERROR: "+event.fault.message);
				}
			);
		}
		
		
		[EventHandler (event="ListeDeDiffusionEvent.SUPPRIMER", properties="listeDeDiffusion")]
		public function remove (dataToRemove:ListeDeDiffusion):void 
		{
			var indexDataToRemove:int = AppModel.listeListesDeDiffusion.getItemIndex(dataToRemove);
			
			serviceHelper.executeServiceCall(listeSrv.deleteListeDeDiffusion(dataToRemove),
				
				function handleRemoveResult(event:ResultEvent, indexDataToRemove:int):void 
				{
					AppModel.listeListesDeDiffusion.removeItemAt(indexDataToRemove);
					//AppModel.listeDeDiffusionCourante = null;
				},
				
				function handleRemoveFault(event:FaultEvent):void 
				{
					trace("ERROR: "+event.fault.message);
				},
				[indexDataToRemove]
			);
		}		
	}
}